package com.zyh.study.service;


import com.zyh.study.pojo.JournalEvent;
import com.zyh.study.pojo.TradeEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @Classname EventProcessor
 * @Description TODO()
 * @Date 2025-09-05 2:18
 * @Created by 86177
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EventProcessor {

    private final AlpacaSseService sseService;

    @EventListener(ContextRefreshedEvent.class)
    public void startProcessing() {
        // 处理Journal事件
        sseService.streamJournalEvents()
                .subscribe(
                        this::handleJournalEvent,
                        this::handleError,
                        () -> log.warn("Journal stream completed")
                );

        // 处理Trade事件
        sseService.streamTradeEvents()
                .subscribe(
                        this::handleTradeEvent,
                        this::handleError,
                        () -> log.warn("Trade stream completed")
                );
    }

    private void handleJournalEvent(JournalEvent event) {
        log.info("Processing journal event: {}", event.getEventId());

        // 根据状态变化处理
        if ("rejected".equals(event.getStatusTo())) {
            log.warn("Journal rejected: {} -> {}", event.getStatusFrom(), event.getStatusTo());
            // 处理拒绝逻辑
        } else if ("processed".equals(event.getStatusTo())) {
            log.info("Journal processed: {}", event.getJournalId());
            // 处理完成逻辑
        } else if ("pending".equals(event.getStatusTo())) {
            log.debug("Journal pending: {}", event.getJournalId());
        }
    }

    private void handleTradeEvent(TradeEvent event) {
        log.info("Processing trade event: {}", event.getEventId());

        // 根据事件类型处理
        switch (event.getEventType()) {
            case "new":
                log.info("New order: {} {} {} @ {}",
                        event.getOrder().getSide(),
                        event.getOrder().getOrderType(),
                        event.getOrder().getSymbol());
                break;

            case "fill":
                log.info("Order filled: {} {} @ {}",
                        event.getOrder().getFilledQty(),
                        event.getOrder().getSymbol(),
                        event.getOrder().getStatus());
                break;

            case "partial_fill":
                log.info("Partial fill: {} of {} {}",
                        event.getOrder().getFilledQty(),
                        event.getOrder().getSymbol());
                break;

            case "canceled":
                log.warn("Order canceled: {} - {}",
                        event.getOrder().getClientOrderId(),
                        event.getOrder().getSymbol());
                break;

            default:
                log.debug("Trade event: {} - {}", event.getEventType(), event.getOrder().getSymbol());
        }
    }

    private void handleError(Throwable error) {
        log.error("Event processing error", error);
        // 这里可以添加错误恢复逻辑
    }
}
